import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:framelib/config/route_config.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:framelib/base/base_view_model.dart';

/**
 * 启动页逻辑处理类
 */
class SplashViewModel extends BaseViewModel<ModuleCommService> with GetTickerProviderStateMixin{
  ModuleCommService commService;
  AnimationController controller;
  Animation<double> animation;

  SplashViewModel({this.controller});
  @override
  ModuleCommService createService()=>(commService=ModuleCommService());


  @override
  void onInit() {
    super.onInit();
    controller = AnimationController(vsync: this, duration: Duration(milliseconds: 2000));
    animation = Tween(begin: 0.0, end: 1.0).animate(controller);
    //上面两行代码表示初始化一个Animation控制器， vsync垂直同步，动画执行时间3000毫秒,然后我们设置一个Animation动画，使用上面设置的控制器
    //监听动画运行状态，当状态为completed时，动画执行结束，跳转首页
    animation.addStatusListener((status){
      if(status == AnimationStatus.completed){
        LogUtil.d('LoginPage-->onModelReady SplashViewModel');
        //先判断是否授权定位
        // checkLocalPermission();
        NavigatorUtil.offAllByName( RouteConfig.loginPage);
      }
    });
    controller.forward(); // 播放动画
  }
  @override
  void dispose() {
    if(controller!=null)controller.dispose();//释放动画
    super.dispose();
  }
  void checkLocalPermission() async{
    // bool checkCommPermission = (await CheckCommPermission.checkCommPermission(Permission.location))??false;
    // if(!checkCommPermission){
    //   if (await Permission.location.isPermanentlyDenied) {
    //     bool isOpenAppSettings = await toOpenAppSettings();
    //   }
    // }else{
    //   NavigatorUtils.push(context, BaseRouter.loginPage,clearStack: true);
    // }
  }
  // Future<bool> toOpenAppSettings() async{
  //   bool isOpenAppSettings = await openAppSettings();
  //   if(isOpenAppSettings){
  //     NavigatorUtils.push(context, BaseRouter.loginPage,clearStack: true);
  //     return true;
  //   }else{
  //     ToastUtils.toast(StringUtils.qdszlmdkyywzsqqx);
  //     toOpenAppSettings();
  //     return false;
  //   }
  // }
}